#!/usr/bin/python3

import configparser
import optparse
import os.path

import classes.fetchers.toolkit as fetchers
import classes.storages as storages
import classes.connects as connects

if __name__ == '__main__':

    config_parser = configparser.SafeConfigParser()
    config_parser.read(os.path.join(os.path.dirname(__file__), 'config.ini'))

    option_parser = optparse.OptionParser()
    option_parser.add_option("--storage-host", type="str", dest="storage_host", default = config_parser.get('storage', 'host'), help="")
    option_parser.add_option("--storage-port", type="int", dest="storage_port", default = config_parser.get('storage', 'port'), help="")
    option_parser.add_option("--storage-user", type="str", dest="storage_user", default = config_parser.get('storage', 'user'), help="")
    option_parser.add_option("--storage-pass", type="str", dest="storage_pass", default = config_parser.get('storage', 'pass'), help="")
    option_parser.add_option("--storage-base", type="str", dest="storage_base", default = config_parser.get('storage', 'base'), help="")
    option_parser.add_option("--path-ingames", type="str", dest="path_ingames", default = config_parser.get('path', 'ingames'), help="")
    option_parser.add_option("--path-archive", type="str", dest="path_archive", default = config_parser.get('path', 'archive'), help="")
    option_parser.add_option("--path-exports", type="str", dest="path_exports", default = config_parser.get('path', 'exports'), help="")
    (options, params) = option_parser.parse_args()

    db = connects.Db(**options.__dict__)

    regions  = storages.Regions (db)
    systems  = storages.Systems (db)
    stations = storages.Stations(db)
    jumps    = storages.Jumps   (db)
    trips    = storages.Trips   (db)
    items    = storages.Items   (db)
    market   = storages.Market  (db)

    print("Starting...")
    db.start()

    print("Dropping...")
    market.drop()
    items.drop()
    trips.drop()
    jumps.drop()
    stations.drop()
    systems.drop()
    regions.drop()

    print("Creating...")
    regions.create()
    systems.create()
    stations.create()
    jumps.create()
    trips.create()
    items.create()
    market.create()

    filedir = os.path.normpath(os.path.join(os.path.dirname(__file__), os.path.expandvars(os.path.expanduser(options.path_ingames))))
    fetcher = fetchers.Toolkit(filedir = filedir)

    print("Importing regions...")
    regions.fulfil(fetcher)

    print("Importing solar systems...")
    systems.fulfil(fetcher)

    print("Importing stations...")
    stations.fulfil(fetcher)

    print("Importing solar system jumps...")
    jumps.fulfil(fetcher)

    print("Importing item types...")
    items.fulfil(fetcher)

    print("Prefilling trips with zero-length paths...")
    trips.prefill()

    print("Committing...")
    db.commit()

    print("Analyzing...")
    regions.analyze()
    systems.analyze()
    stations.analyze()
    jumps.analyze()
    trips.analyze()
    items.analyze()
    market.analyze()

    print("Done!")
